import $ from 'jquery'

export const bossService = {
    /*
    * 采集的信息
    *  「
    *
    *   教育经历、工作经历
    * 姓名、手机号、邮箱、性别、年龄、学历、城市、公司、职位、学校、专业、行业、标签、工作年限、期望薪资
    *
    * 」
    * */
    // 采集开始
    collection() {
        console.log('*** boss ***');

        let base = getBasicInfo()
        let desired = getDesired()
        let eduction = getEducation()
        let work = getWorkInfo()
        let pro = getProInfo()
        let evaluation = getEvaluation()
        let other = getOther()
        let htmlFile = $util.htmlToString(document) // 获取源html代码

        // 自定义工具函数
        function $find(selector) {
            let dialog = $(".resume-dialog")
            if (dialog.length !== 0) {
                return dialog.find(selector)
            }
            if ($(".sync-container iframe").length > 0) {
                return $(".sync-container iframe").contents().find(selector)
            }
            if ($("#recommendContent iframe").length > 0) {
                return $("#recommendContent iframe").contents().find(selector)
            }
            return $(selector)
        }

        // 基本信息
        function getBasicInfo() {
            // 姓名
            let name = $find('.name .geek-name').text().trim()
            //  头像
            let imgUrl = $find(".item-base .figure img").prop("currentSrc")
            // 在职状态
            let status = $find('.item-base .info-labels span').eq(4).text().trim()
            // 性别
            let sex = ''
            let sexClass = $find('.item-base .name i').attr('class')
            if (sexClass.includes('women')) {
                sex = '女'
            } else if (sexClass.includes('man')) {
                sex = '男'
            }
            // 年龄
            let age
            let ageStrReg = $find('.item-base .info-labels span').eq(1).text().trim().match(/\d+/)
            age = ageStrReg ? ageStrReg[0] : ''
            // 工作年限
            let workExperience = $find('.item-base .info-labels span').eq(2).text().trim()
            // 学历
            let qualifications = $find('.item-base .info-labels span').eq(3).text().trim()

            //  婚姻状态
            let maritalStatus = ''

            return {
                name: name, // 姓名
                sex: sex, // 性别
                imgUrl: imgUrl, // 头像
                status: status, //在职状态
                age: age, // 年龄
                workExperience: workExperience,// 工作年限
                qualifications: qualifications,// 学历
                nowCity: '',// 当前所在城市
                currentSalary: '',// 当前薪资
                currentIndustry: '',// 当前行业
                skills: "" ,// 技能标签
                photoUrl: imgUrl,
                maritalStatus: maritalStatus // 婚姻状态
            }
        }

        // 职业期望
        function getDesired() {
            let tempText = $find('.resume-item').eq(1).find('.info-labels span')
            // 行业
            let industries = tempText.eq(1).text().trim() || ''
            // 期望职位
            let desiredJob = tempText.eq(0).text().trim() || ''
            // 期望年薪
            let desiredSalary = tempText.eq(2).text().trim()

            return {
                desiredCitys: '', // 期望城市
                desiredJobName: desiredJob, // 期望职位
                desiredSalary: desiredSalary, // 期望薪资是个范围 例如 1-2k 12薪
                industries: industries // 期望行业
            }
        }

        //  工作经历
        function getWorkInfo() {
            //
            /*
              * companyName: 公司名,
              * companyDesc: 描述,
              * jobName: 职位,
              * department: 部门,
              * leader: 汇报对象,
              * areaType: 'sys' 所在地区,
              * areaId: 所在地区,
              * subordinateCount: 下属人数,
              * workStart: 开始时间,
              * workEnd: 结束时间,
              * yearlySalary: 年薪,
              * yearlySalaryRemark: 年薪备注,
              * duty: 职责
              * achievement: 业绩,
              * leavingReason: 离职原因
              * */
            let work = []
            let workEls
            $find('.resume-item').each(function () {
                if ($(this).find('.title').text().indexOf('工作') !== -1) {
                    workEls = $(this).find('.history-item')
                }
            })
            workEls && workEls.each(function () {
                let detail = $(this)
                let workTime = detail.find('.period').text().trim().split('-')
                let workStart = workTime[0].replace('.', '-').trim();
                let workEnd = workTime[1].replace('.', '-');
                let company = detail.find('.name').text().trim().split(/\s+/g)[0]
                let companyDesc = detail.find('.short-text').text().trim()

                let duty = detail.find('.item-text .text').eq(0).text().trim()
                if (detail.find('.item-text .text').length && detail.find('.item-text .text').length > 1) {
                    duty = detail.find('.item-text .text').eq(1).text().trim()
                }
                let jobName = detail.find('.name').text().trim().split(/\s+/g)[1]
                let salary = ''

                let item = {
                    company: company, // 公司名称
                    jobName: jobName, // 职位名称
                    area: '', // 公司所在地区
                    category: '', // 职位类别
                    department: '', // 所在部门
                    leader: '', // 领导-汇报对象
                    subordinateCount: 0, //  下属人数
                    startTime: workStart, // 工作时间起
                    endTime: workEnd, // 工作时间止
                    jobSalary: salary, // 薪资待遇
                    workDescription: duty, // 职责业绩
                    leavingReason: '' // 离职原因
                }

                work.push(item)
            })
            return work
        }

        //  项目经历
        function getProInfo() {
            let proInfo = []
            let proEls
            $find('.resume-item').each(function () {
                if ($(this).find('.title').text().indexOf('项目') !== -1) {
                    proEls = $(this).find('.history-item')
                }
            })
            proEls && proEls.each(function () {
                // companyName: '', // 公司名称
                //     jobName: '', // 职位名称
                //     projectName: '', // 项目名称
                //     projectDesc: '', // 项目描述
                //     projectDuty: '', // 项目职责
                //     projectAchievement: '', //  项目业绩
                //     projectStart: null, // 项目开始时间
                //     projectEnd: null, // 项目结束时间
                let detail = $(this)
                let data = {
                    companyName: '',
                    jobName: '',
                    name: '',
                    description: '',
                    content: '',
                    projectAchievement: '',
                    startTime: null, // 项目开始时间
                    endTime: null, // 项目结束时间
                }

                let projectName = detail.find('.name').html().replace('<em class="vline"></em>', ' ')
                projectName = $('<p>' + projectName + '</p>').text().trim().split(/\s+/g)
                data.name = projectName[0]
                data.jobName = projectName[1]
                let time = detail.find('.period').text().trim().split('-')
                data.startTime = time[0].replace('.', '-').trim();
                data.endTime = time[1].replace('.', '-');
                data.content = detail.find('.item-text .text').eq(0).text().trim()
                if (detail.find('.item-text .text').length && detail.find('.item-text .text').length > 1) {
                    data.projectAchievement = data.content
                    data.content = detail.find('.item-text .text').eq(1).text().trim()
                }
                proInfo.push(data)
            })
            return proInfo
        }

        // 教育经历
        function getEducation() {
            /*
               * schoolName: 学校名称
               * majorName专业名称
               * majorName: 开始时间
               * startDate: 开始时间
               * endDate: 结束时间
               * qualifications: 学历
               * theEntranceExamination: 是否统招
               * desc: 描述
               * */
            let education = []
            let educationEls
            $find('.resume-item').each(function () {
                if ($(this).find('.title').text().indexOf('教育') !== -1) {
                    educationEls = $(this).find('.history-item')
                }
            })
            educationEls && educationEls.each(function () {
                let detail = $(this)
                let theEntranceExamination = ''
                let desc = detail.find('.text').text().trim()
                let baseInfo = detail.find('.name').text().trim().split(/\s+/g)
                let schoolName = baseInfo[0].trim() // 学校名称
                let majorName = baseInfo[1].trim() // 专业
                let qualifications = baseInfo[2] || '' // 学历
                qualifications = qualifications.trim().split(' ')[0]
                let time = detail.find('.period').text().trim().split('-') // 起止时间
                let startDate = time[0].replace('.', '-').trim();
                let endDate = time[1].replace('.', '-');
                // 是否统招
                if (qualifications.indexOf('统招') !== -1) {
                    theEntranceExamination = 1
                }

                let item = {
                    school: schoolName,
                    majorName: majorName,
                    description: desc,
                    startTime: startDate,
                    endTime: endDate,
                    qualifications: qualifications,
                    theEntranceExamination: theEntranceExamination,
                    education: $util.getEducation(qualifications)
                }
                education.push(item)
            })
            return education
        }

        // 自我评价
        function getEvaluation() {
            return $('.item-base .selfDescription').text().trim()
        }

        // 其他 -- 语言能力、证书
        function getOther() {
            // 语言
            let language = ''
            // 证书
            let cert = []

            let educationEls = $find('.resume-certificate').find('h3').text()
            if(educationEls.includes('证书')) {
                $find('.resume-certificate li').each(function () {
                    cert.push($(this).text().trim())
                })
            }
            cert = cert.join(',')
            return {
                language: language,
                cert: cert
            }
        }

        return {
            href: location.href,
            base: base,
            desired: desired,
            workList: work,
            projectList: pro,
            educationList: eduction,
            other: other,
            evaluation: evaluation,
            htmlFile: htmlFile,
            htmlFileName: 'BOSS直聘_' + Date.now() + '.html'
        }
    },
}

